//index.js
//获取应用实例
const app = getApp()
const {
  wxLogin,
  getUserInfo
} = require('../../utils/wxPromise')
import {
  request
} from '../../utils/wxRequest'
import {
  auth
} from '../../utils/auth'
Page({
  data: {
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  async getUserInfo(e) {
    let formData = {}
    let detail = e.detail;
    if (detail.userInfo) {
      let userInfo = await getUserInfo()
      console.log(userInfo)
      let {
        code
      } = await wxLogin()
      formData.code = code
      formData.encryptedData = userInfo.encryptedData
      formData.iv = userInfo.iv
      console.log(formData)
      let {
        data
      } = await request({
        url: 'http://127.0.0.1:3000/auth/wxlogin',
        method: "post",
        data: formData
      })
      if (data.token) {
        wx.setStorageSync('state', {
          expiredDate: Date.now() + 60 * 60 * 1000 /*缓存一个小时*/ ,
          user: data
        })
      }


      // let data = await auth(e.detail)
      //   console.log(e)
    } else {
      wx.showToast({
        title: "拒绝授权",
        icon: 'none'
      })

    }
    console.log(e)
  },
  async onLoad() {
    // let state = wx.getStorageSync('state')
    // if(state){
    //     this.setData({
    //         userInfo: state.data.userInfo
    //     })
    // }
  }
})